(ns acst.models.blogs
  (:use acst.config.error-config)
  (:require [clojure.java.jdbc :as sql]
            [acst.models.db :as db]
            [acst.utils.sql-util :as sqlutil]
            [acst.utils.core :as core]))

(defn register-blog! [user_id]
  (db/insert! :blogs
              {:owner_id user_id}))

(defn find-blogs
  ([limit page] (db/query (:blog-not-found blogs-error) ["SELECT * FROM blogs ORDER BY blog_id LIMIT ? OFFSET ?" limit (* (- page 1) limit)]))
  ([where-set] (-> (as-> (condp = (keys where-set)
                           '(:user_id)
                           ["owner_id = ?" (where-set :user_id)]
                           '(:blog_id)
                           ["blog_id = ?" (where-set :blog_id)]) $
                         (core/conj-all [(str "SELECT * FROM blogs WHERE " (first $))] (rest $)))
                   (db/query (:blog-not-found blogs-error)))))